package com.whccb.apps.office.document.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.actionsoft.apps.AppsConst;
import com.actionsoft.apps.lifecycle.api.AppsAPIManager;
import com.actionsoft.apps.resource.AppContext;
import com.actionsoft.bpms.bo.design.cache.BOCache;
import com.actionsoft.bpms.bo.design.model.BOItemModel;
import com.actionsoft.bpms.bo.design.model.BOModel;
import com.actionsoft.bpms.commons.amc.util.AMCUtil;
import com.actionsoft.bpms.commons.dictionary.model.DictModel;
import com.actionsoft.bpms.commons.dictionary.util.Xml2DictUtil;
import com.actionsoft.bpms.commons.dictionary.util.XmlUtil;
import com.actionsoft.bpms.ui.base.AbstractUIComponent;
import com.actionsoft.bpms.ui.base.UIContext;
import com.actionsoft.bpms.util.Html;
import com.actionsoft.i18n.I18nRes;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;

public class UserGroupUI extends AbstractUIComponent {

	public UserGroupUI(UIContext context) {
		super(context);
	}

	/**
	 * 备注信息
	 */
	public String getConfigWebPage(UIContext context) {
		String categoryName = "";
		BOItemModel boItemModel = context.getBoItemModel();
		BOModel boModel = BOCache.getInstance().getModel(
				context.getBoItemModel().getBoModelId());

		Map<String, Object> macroLibraries = new HashMap<String, Object>();

		String appId = "";
		if (boModel == null) {
			appId = AppsConst.SYS_APP_PLATFORM;
		} else {
			appId = boModel.getAppId();
			categoryName = boModel.getCategoryName();
		}
		macroLibraries.put("categoryName", categoryName);
		AppContext appContext = SDK.getAppAPI().getAppContext(appId);

		List<AppContext> list = AppsAPIManager.getInstance()
				.queryDependenciesIncludeSelfAndSort(appContext);
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		int cnt = 1;// 计数
		for (AppContext appContext2 : list) {
			List<String> fileName = XmlUtil.getFileName(appContext2.getId(),
					true);
			for (String string : fileName) {
				DictModel oldModel = Xml2DictUtil.getModel(appContext2.getId(),
						string, true);
				String dictName = oldModel.getTitle();
				Map<String, String> value = new HashMap<String, String>();
				String img = AMCUtil.getAppIconNoClass(appContext2.getId());
				value.put("label", dictName + "   (" + appContext2.getName()
						+ ")");
				value.put("value", string);
				value.put("imgs", img);
				value.put("id", string);
				value.put("title", dictName);
				value.put("categoryName", categoryName);
				value.put("sourceAppId", appContext2.getId());
				data.add(value);
				cnt++;
			}
		}
		macroLibraries.put("fileName",
				cnt == 1 ? "[]" : JSONArray.fromObject(data).toString());
		macroLibraries.put("appId", appId);

		return getCommonConfigWebPage(context, macroLibraries);
	}

	// 生成初始化脚本
	public String getInitScript(UIContext context, boolean readonly) {
		String appId = context.getFormModel().getAppId();
		BOItemModel boItemModel = context.getBoItemModel();
		StringBuilder initScript = new StringBuilder();
		JSONObject config = getConfigJson(boItemModel, context);
		String dicTitle = I18nRes
				.findValue(appId, config.getString("dicTitle"));
		config.put("dicTitle", dicTitle);
		config.put("boId", boItemModel.getBoModelId());
		initScript.append("UIInit.initUIComponentGridDictionary('"
				+ boItemModel.getName() + "',"
				+ Html.encodeForJSON(config.toString()) + ");\n");
		return initScript.toString();
	}

	/**
	 * 运行时UI组件代码
	 */
	public String getUI(UIContext context, boolean readonly) {
		StringBuilder html = new StringBuilder();
		boolean isMobile = context.getUserContext().isMobileClient();
		BOItemModel boItemModel = context.getBoItemModel();
		JSONObject configJson = getConfigJson(boItemModel, context);
		String value = context.getValue();
		String type = "text";
		String innerClass = getInnerHtml(configJson, boItemModel, "txt dict",
				"mui-input-right", isMobile, readonly);
		if (readonly) {
			html.append(getReadonlyUI(context));
			return html.toString();
		} else {
			String mode = "";
			if (configJson.containsKey("mode")) {
				mode = configJson.getString("mode");// 模式：对话框模式，下拉模式
			} else {
				mode = "dialog";
			}
			StringBuilder isNotNull = new StringBuilder();
			if (isMobile) {
				html.append("<div class='mui-icon-div'>");
				html.append(
						"<input type='" + type + "' " + innerClass + " name='")
						.append(boItemModel.getName()).append("' ")
						.append(" id='").append(boItemModel.getName() + "' ")
						.append(" value=\"").append(value).append("\">");
				html.append("<span class='mui-icon-span'><span class='mui-icon mui-input-right'></span></span></div>");
				html.append("</div>");
				if (boItemModel.isNullable() == false) {
					isNotNull.append("<span class='required'>")
							.append(html.toString()).append("</span>");
					return isNotNull.toString();
				}
			} else {
				if (boItemModel.isNullable() == false) {
					html.append("<span class='required'>");
				}
				html.append(
						"<input type='" + type + "' " + innerClass + " name='")
						.append(boItemModel.getName())
						.append("'  title='")
						.append(SDK.getRuleAPI().executeAtScript(
								boItemModel.getTooltip(), context)).append("'")
						.append(" id='").append(boItemModel.getName() + "' ")
						.append(" value=\"").append(value).append("\">");
				if (boItemModel.isNullable() == false) {
					html.append("</span>");
				}
				if (!"dialog".equals(mode)) {
					html.append("<div id='popbox_"
							+ boItemModel.getName()
							+ "' class='awsui-popbox' style='display:none;border: 1px solid #C0C2C4;' >");
					html.append("<iframe name='popbox_"
							+ boItemModel.getName()
							+ "_frm' id='popbox_"
							+ boItemModel.getName()
							+ "_grid_frm'  frameborder='0' style='width:100%;height:100%' src=''></iframe></div>");
				}
			}
		}
		return html.toString();
	}

	public String getValidateScript(UIContext context, boolean readonly) {
		BOItemModel boItemModel = context.getBoItemModel();
		JSONObject configJson = super.getConfigJson(boItemModel, context);
		String boName = "";
		BOModel model = BOCache.getInstance().getModel(
				boItemModel.getBoModelId());
		if (model != null) {
			boName = model.getName();
		}
		return "UIValidate.validateUIGridDictionary('" + boName + "','"
				+ boItemModel.getName() + "','"
				+ getI18NValue(context, boItemModel.getTitle()) + "',"
				+ Html.encodeForJSON(configJson.toString())
				+ ",cellvalue, record)\n";
	}
}
